<template>
  <van-cell title="昵称" is-link to="/user/edit" :value="user.username"
            @click="toEdit('username','昵称',user.username)"/>
  <van-cell title="头像" is-link to="/user/edit">
    <img width="40px" height="40px" :src="user.avatarUrl"/>
  </van-cell>
  <van-cell title="性别" is-link to="/user/edit" :value="user.gender==1?'男':'女'"
            @click="toEdit('gender','性别',user.gender.toString())"/>
  <van-cell title="手机号" is-link to="/user/edit" :value="user.phone"
            @click="toEdit('phone','手机号',user.phone)"/>
  <van-cell title="邮箱" is-link to="/user/edit" :value="user.email"
            @click="toEdit('email','邮箱',user.email)"/>

</template>

<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import {userType} from "../models/user";
import {showFailToast, showSuccessToast} from "vant";
import {getCurrentUser} from "../services/user.ts";

/*const user = {
  id: 1,
  username: 'lyxnb',
  userAccount: 'lyx',
  avatarUrl: 'https://pic2.zhimg.com/v2-af7a97397d2940f107656415a2bbbf89_r.jpg?source=w',
  gender: '男',
  phone: '1234321',
  email: '1211@1630.com',
}*/
const router = useRouter()
const user = ref<userType>({
  avatarUrl: "",
  createTime: new Date(),
  email: "",
  gender: 0,
  id: 0,
  phone: "",
  profile: "",
  tags: [],
  userAccount: "",
  userPassword: "",
  userRole: 0,
  username: ""
})

onMounted(async () => {
  const res: userType | null = await getCurrentUser()
  if (res) {
    user.value = res
    showSuccessToast('获取用户信息成功')
  } else {
    showFailToast('获取用户信息失败')
  }

})

const toEdit = (editKey: string, editName: string, currentValue: string) => {
  router.push({
    path: '/user/edit',
    query: {
      editKey,
      editName,
      currentValue,
    },
  })
}
</script>

<style scoped>

</style>
